<?php
/**
 * 延期订单类
 * @author 志杨
 *
 */
class delayOrder
{
	var $db;
	var $tpl;
	var $page;
	var $selectSql;
	var $delayDay;
	
	function delayOrder()
	{
		global $db;
		
		global $tpl;		
		
		global $order;
		$this -> db     = & $db;
		$this -> tpl    = & $tpl;
		if(isset($_GET['time']) && $_GET['time']>0){
			$time = $_GET['time'];
		}else{
			$time = time();
		}
		if(isset($_GET['delay_day']) && $_GET['delay_day']>0){
			$timeStr = $this->get_delay_time_str($time,$_GET['delay_day']);
			$this->delayDay = $_GET['delay_day'];
		}else{
			$timeStr = $this->get_delay_time_str($time);
			$this->delayDay = 3;
		}
		$str = " and a.pay_time<'{$timeStr}'";
		if($_GET['code'])
		{
			$str .=" and  a.order_id like '$_GET[code]%' ";
		}
		
		//----------------------分仓
		$general_shop =$order->get_general_shop();
		
		if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
			msg('noright.php');
			exit;
		}
		if ($general_shop){
			$_GET['stock_shop'] =$general_shop;
		}
		if ($_GET['stock_shop']){
			$str.=" and stock_shop = '$_GET[stock_shop]' ";
		}
		//-----------------
		
		
		$str.=" and  a.status ='2' ";
		$this->selectSql="select a.member_id,a.order_type,a.status,a.print_status,a.order_id,a.client_system,a.client_platform,(cost+freight+tax_price+insurance_fee-cpns_price-discount) as sum,discount,cpns_price,cost,freight,tax_price,insurance_fee,(select username from ".MEMBER." where userid=a.member_id) user,(select name from ".MEMBER." where userid=a.member_id) nick_name,ship_name,order_time,pay_time,ship_time,finished_time,pay_status,ship_status,shipping_name,payment_name,shipping_no from ".ORDER." a  where 1 $str order by a.order_time ";
	}
	
	//获取订单列表
	function get_order_list(){
		$sql = $this->selectSql;
		//=============================
	  	$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$this->db->query($sql);
			$de['count']=$page->totalRows = $this->db->num_rows();
		}
        $sql .= "  limit ".$page->firstRow.",".$page->listRows;
		//=====================
		$this->db->query($sql);
		$ss=$this->db->getRows();
		foreach($ss as $k)
		{
			$k['order_ship_status']=$order_ship_status[$k['status']-1];
			$now = time();
			//如果支付时间为周末，则把它置为周一0点
			$order_w = date('w',$k['pay_time']);
			if($order_w==0){
				$k['pay_time'] = strtotime(date('Y-m-d 23:59:59',$k['pay_time']))+1;
			}elseif($order_w==6){
				$k['pay_time'] = strtotime(date('Y-m-d 23:59:59',$k['pay_time']))+86401;
			}
			$weekDays = $this->get_weekend_days(date('Y-m-d',$k['pay_time']), date('Y-m-d',$now));
			$delayTime = $now - $this->delayDay*86400;
			$restTime = $delayTime - $k['pay_time'];
			$k['delay_time'] = $this->formatShowTime($restTime);
			$list[]=$k;
		}
		$de["list"]=$list;
		$de["page"]=$page->prompt();
		return $de;
	}
	/**
	 * 获取延期订单总数
	 */
	function getTotalNum(){
		$this->db->query($this->selectSql);
		$count = $this->db->num_rows();
		
		return $count;
	}
	/**
	 * 相对当前时间获取延期的时间戳
	 * @param string $time 时间戳
	 * @param int	 $delayDay  延期天数基值(几天算延期)
	 */
	function get_delay_time_str($time,$delayDay=3){
		$timeStr = 0;
		$w = date('w',$time);
		$end_time = strtotime(date('Y-m-d 23:59:59',$time));//当前传入时间$time的当天23:59:59的时间戳
		switch ($delayDay)
		{
			case 1:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+2)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 2:
						$timeStr = $time-$delayDay*86400;
						break;
					case 3:
						$timeStr = $time-$delayDay*86400;
						break;
					case 4:
						$timeStr = $time-$delayDay*86400;
						break;
					case 5:
						$timeStr = $time-$delayDay*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+1)*86400;
						break;
				}
				break;
			case 2:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+2)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 2:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 3:
						$timeStr = $time-$delayDay*86400;
						break;
					case 4:
						$timeStr = $time-$delayDay*86400;
						break;
					case 5:
						$timeStr = $time-$delayDay*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+1)*86400;
						break;
				}
				break;
			case 3:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+2)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 2:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 3:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 4:
						$timeStr = $time-$delayDay*86400;
						break;
					case 5:
						$timeStr = $time-$delayDay*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+1)*86400;
						break;
				}
				break;
			case 4:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+2)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 2:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 3:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 4:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 5:
						$timeStr = $time-$delayDay*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+1)*86400;
						break;
				}
				break;
			case 5:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+4)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 2:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 3:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 4:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 5:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+3)*86400;
						break;
				}
				break;
			case 6:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+4)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+4)*86400;
						break;
					case 2:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 3:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 4:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 5:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+3)*86400;
						break;
				}
				break;
			case 7:
				switch ($w)
				{
					case 0:
						$timeStr = $end_time-($delayDay+4)*86400;
						break;
					case 1:
						$timeStr = $time-($delayDay+4)*86400;
						break;
					case 2:
						$timeStr = $time-($delayDay+4)*86400;
						break;
					case 3:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 4:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 5:
						$timeStr = $time-($delayDay+2)*86400;
						break;
					case 6:
						$timeStr = $end_time-($delayDay+3)*86400;
						break;
				}
				break;
		}
		
		return $timeStr;
	}
	
	/**
	 * | Author: zzy
	 * | @param char|int $start_date 一个有效的日期格式，例如：20091016，2009-10-16
	 * | @param char|int $end_date 同上
	 * | @param int $weekend_days 一周休息天数
	 * | @return array
	 * | array[total_days]  给定日期之间的总天数
	 * | array[total_relax] 给定日期之间的周末天数
	 */
	function get_weekend_days($start_date, $end_date, $weekend_days=2) {
	
		$data = array();
		if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date);
	
		$start_reduce = $end_add = 0;
		$start_N      = date('N',strtotime($start_date));
		$start_reduce = ($start_N == 7) ? 1 : 0;
	
		$end_N = date('N',strtotime($end_date));
	
		// 进行单、双休判断，默认按双休计算
		$weekend_days = intval($weekend_days);
		switch ($weekend_days)
		{
			case 2:
				in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1;
				break;
			case 1:
			default:
				$end_add = ($end_N == 7) ? 1 : 0;
				break;
		}
	
		$days = round(abs(strtotime($end_date) - strtotime($start_date))/86400) + 1;
		$data['total_days'] = $days;
		$data['total_relax'] = floor(($days + $start_N - 1 - $end_N) / 7) * $weekend_days - $start_reduce + $end_add;
		$data['total_work'] = $data['total_days'] - $data['total_relax'];
		
		return $data;
	}
	
	/**
	 * 延期显示时间     XX分钟   XX小时 XX天
	 * @param unknown $restTime
	 * @return Ambigous <string, unknown>
	 */
	public static function formatShowTime($restTime)
	{
		$result = "";
		if($restTime>86400){
			$result = floor($restTime/86400)."天";
			$rest = $restTime%86400;
			if($rest<3600){
				$result .= ceil($rest/60)."分钟";
			}else{
				$result .= ceil($rest/3600)."小时";
			}
		}elseif($restTime<3600){
			$result = ceil($restTime/60)."分钟";
		}else{
			$result = ceil($restTime/3600)."小时";
		}
		return $result;
	}
}
?>
